package com.park.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.park.model.PosChargeData;
import com.park.model.Poschargemac;
import com.park.service.HardwareService;
import com.park.service.JedisClient;
import com.park.service.PosChargeDataService;
import com.park.service.PosChargeMacService;
import com.park.service.Utility;

@Controller
@RequestMapping("poschargemac")
public class PosChargeMacController {

	@Autowired
	PosChargeMacService poschargemacService;
	@Autowired
	PosChargeDataService poschargedataService;
	@Autowired
	HardwareService hardwareService;
	@Resource(name = "jedisClient")
	private JedisClient jedisClient;

	private static final Log logger = LogFactory.getLog(PosChargeMacController.class);

	@RequestMapping(value = "getLastByMac", method = RequestMethod.POST, produces = {
			"application/json;charset=UTF-8" })
	@ResponseBody
	public String getLastByMac(@RequestBody Map<String, String> args) {
		logger.info(args.toString());
		String mac = args.get("mac");
//		int macId=hardwareService.macToId(mac);
//		List<Poschargemac> poschargemacs=poschargemacService.selectByMac(macId, 1);

		Map<String, Object> ret = new HashMap<String, Object>();
		String poschargeid = jedisClient.get("out" + mac);
		if (poschargeid == null) {
			return Utility.gson.toJson(ret);
		}

		PosChargeData posChargeData = poschargedataService.getById(Integer.parseInt(poschargeid));
		try {
			ret.put("status", 1001);
			ret.put("body", posChargeData);
		} catch (Exception e) {
			// TODO: handle exception
		}
		logger.info(
				mac + " " + poschargeid + "-" + posChargeData.getCardNumber() + "-" + posChargeData.getChargeMoney());

		return Utility.gson.toJson(ret);
	}

	@RequestMapping(value = "getLastByMacV2", method = RequestMethod.POST, produces = {
			"application/json;charset=UTF-8" })
	@ResponseBody
	public String getLastByMacV2(@RequestBody Map<String, String> args) {
		logger.info(args.toString());
		String mac = args.get("mac");
//		int macId=hardwareService.macToId(mac);
//		List<Poschargemac> poschargemacs=poschargemacService.selectByMac(macId, 1);

		Map<String, Object> ret = new HashMap<String, Object>();
		String poschargeid = jedisClient.get("out" + mac);
		if (poschargeid == null) {
			return Utility.gson.toJson(ret);
		}

		PosChargeData posChargeData = poschargedataService.getById(Integer.parseInt(poschargeid));
		if ((posChargeData.getPayType() == 2 || posChargeData.getPayType() == 9 || posChargeData.getPayType() == 30) && 
				(posChargeData.getOperatorId().contains("operator") || posChargeData.getOperatorId().contains("超时15分钟"))&&
				posChargeData.getChargeMoney()>0) {
			try {
				ret.put("status", 1001);
				ret.put("body", posChargeData);
			} catch (Exception e) {
				// TODO: handle exception
			}
		} else {
			ret.put("status", 1002);
		}
		logger.info(
				mac + " " + poschargeid + "-" + posChargeData.getCardNumber() + "-" + posChargeData.getChargeMoney());

		return Utility.gson.toJson(ret);
	}
}
